from src.llamafactory.hparams import read_args, get_ray_args, get_train_args
from transformers import HfArgumentParser
from src.llamafactory.hparams import ModelArguments, DataArguments, TrainingArguments, FinetuningArguments, \
    GeneratingArguments


# print(
#     ray_args)  # RayArguments(ray_run_name=None, ray_storage_path='./saves', ray_num_workers=1, resources_per_worker={'GPU': 1}, placement_strategy='PACK')

def _parse_args(
        parser, args=None, allow_extra_keys: bool = False
):
    args = read_args(args)
    if isinstance(args, dict):
        return parser.parse_dict(args, allow_extra_keys=allow_extra_keys)

    (*parsed_args, unknown_args) = parser.parse_args_into_dataclasses(args=args, return_remaining_strings=True)

    if unknown_args and not allow_extra_keys:
        print(parser.format_help())
        print(f"Got unknown args, potentially deprecated arguments: {unknown_args}")
        raise ValueError(f"Some specified arguments are not used by the HfArgumentParser: {unknown_args}")

    return tuple(parsed_args)

_TRAIN_ARGS = [ModelArguments, DataArguments, TrainingArguments, FinetuningArguments, GeneratingArguments]
def _parse_train_args(args):
    parser = HfArgumentParser(_TRAIN_ARGS)
    # 不允许出现未知参数
    allow_extra_keys = False

    # 读取配置获得字典类参数集合
    args = read_args(args)
    # 如果确定是字典信息就返回 huggingface 的配置解析信息,一般都走这个逻辑.
    if isinstance(args, dict):
        print("读取字典,解析完成")
        return parser.parse_dict(args, allow_extra_keys=allow_extra_keys)
    # 如果不是从文件中读取的参数,就走这个逻辑,主要解决使用命令行配置的方式来进行训练过程,包含-h的帮助流程.
    (*parsed_args, unknown_args) = parser.parse_args_into_dataclasses(args=args, return_remaining_strings=True)
    # python 03_help信息打印.py --num_beams 100 --model_name_or_path "data" --skip_special_tokens true
    # print("parsed_args",parsed_args)

    # 是否要进行未知参数检查
    if unknown_args and not allow_extra_keys:
        print(parser.format_help())
        print(f"Got unknown args, potentially deprecated arguments: {unknown_args}")
        raise ValueError(f"Some specified arguments are not used by the HfArgumentParser: {unknown_args}")
    print("解析完成")
    # return _parse_args(parser, args, allow_extra_keys=allow_extra_keys)

#  python 03_help信息打印.py examples/train_lora/llama3_lora_sft.yaml  user=tom 这里会报错
args = read_args()
_parse_train_args(args)